﻿using FuiouMerchantAccess;
using FuiouMerchantAccess.Models.Request;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;

namespace fuiou.Controllers {
    public class MerchantAccessController : Controller {
        readonly ILogger logger;
        readonly Config config;

        public MerchantAccessController(ILogger<MerchantAccessController> _logger, IOptions<Config> _config) {
            logger = _logger;

            if (_config != null && _config.Value != null) {
                config = _config.Value;
            }
        }

        public IActionResult Index() {
            return View();
        }

        /// <summary>
        /// 商户信息更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult WxMchntEdit(WxMchntEditRequestInfo request) {
            try {
                MerchantAccess merchantAccess = new MerchantAccess(config);

                request.TraceNo = $"{DateTime.Now:yyMMddHHffff}";

                var response = merchantAccess.WxMchntEdit(request);

                return Ok(new {
                    response,
                    traceNo = request.TraceNo
                });
            } catch (Exception ex) {
                return Ok(new {
                    error = ex.Message, innerError = ex.InnerException != null ? ex.InnerException.Message : ""
                });
            }
        }

        /// <summary>
        /// 商户名称判重
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult WxMchntNameCheck(WxMchntNameCheckRequestInfo request) {
            try {
                MerchantAccess merchantAccess = new MerchantAccess(config);

                request.TraceNo = $"{DateTime.Now:yyMMddHHffff}";

                var response = merchantAccess.WxMchntNameCheck(request);

                return Ok(new {
                    response,
                    traceNo = request.TraceNo
                });
            } catch (Exception ex) {
                return Ok(new {
                    error = ex.Message, innerError = ex.InnerException != null ? ex.InnerException.Message : ""
                });
            }
        }

        /// <summary>
        /// 商户渠道子商户号查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IActionResult ChnlSubMchIdQuery(ChnlSubMchIdQueryRequestInfo request) {
            try {
                MerchantAccess merchantAccess = new MerchantAccess(config);

                request.TraceNo = $"{DateTime.Now:yyMMddHHffff}";

                var response = merchantAccess.ChnlSubMchIdQuery(request);

                return Ok(new {
                    response,
                    traceNo = request.TraceNo
                });
            } catch (Exception ex) {
                return Ok(new {
                    error = ex.Message, innerError = ex.InnerException != null ? ex.InnerException.Message : ""
                });
            }
        }
    }
}
